package com.hdax.dm.service;

import com.hdax.dm.common.constant.ConstantData;
import com.hdax.dm.common.exception.DmException;
import com.hdax.dm.common.exception.DmUserLoginException;
import com.hdax.dm.entity.user.DmUser;
import com.hdax.dm.feign.user.DmUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DmUserLoginService {

    @Autowired
    private DmUserFeign dmUserFeign;

    //短信验证
    public DmUser msglogin(String phoneNumber) throws DmException {
        if(phoneNumber == null || phoneNumber.trim().equals("")){
            throw new DmUserLoginException(ConstantData.ErrCode.NULL_PHONE_CODE,ConstantData.ErrMsg.NULL_PHONE_MSG);
        }
        DmUser dmUser = dmUserFeign.findByPhone(phoneNumber);
        return dmUser;
    }

    //密码验证
    public DmUser login(String phoneNumber,String password) throws DmException {
        if(phoneNumber == null || phoneNumber.trim().equals("")){
            throw new DmUserLoginException(ConstantData.ErrCode.NULL_PHONE_CODE,ConstantData.ErrMsg.NULL_PHONE_MSG);
        }
        DmUser dmUser = dmUserFeign.findByPhone(phoneNumber);
        if(!dmUser.getPassword().equals(password)){
            throw new DmUserLoginException(ConstantData.ErrCode.NULL_PASSWORD_CODE,ConstantData.ErrMsg.NULL_PASSWORD_MSG);
        }
        return dmUser;
    }
}
